/*
 * Sonatype Nexus (TM) Open Source Version
 * Copyright (c) 2008-present Sonatype, Inc.
 * All rights reserved. Includes the third-party code listed at http://links.sonatype.com/products/nexus/oss/attributions.
 *
 * This program and the accompanying materials are made available under the terms of the Eclipse Public License Version 1.0,
 * which accompanies this distribution and is available at http://www.eclipse.org/legal/epl-v10.html.
 *
 * Sonatype Nexus (TM) Professional Version is available from Sonatype, Inc. "Sonatype" and "Sonatype Nexus" are trademarks
 * of Sonatype, Inc. Apache Maven is a trademark of the Apache Software Foundation. M2eclipse is a trademark of the
 * Eclipse Foundation. All other trademarks are the property of their respective owners.
 */
package org.sonatype.nexus.repository.vulnerability;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import javax.annotation.Nullable;

/**
 * Information about component vulnerabilities.
 *
 * @since 3.24
 */
public class ComponentsVulnerability
{
  private final Map<AuditComponent, List<Vulnerability>> components = new HashMap<>();

  public Map<AuditComponent, List<Vulnerability>> getAuditComponents() {
    return components;
  }

  /**
   * Add vulnerability to the component.
   *
   * @param auditComponent {@link AuditComponent} with vulnerability.
   * @param vulnerability  {@link Vulnerability} of the component.
   */
  public void addVulnerability(final AuditComponent auditComponent, @Nullable final Vulnerability vulnerability) {
    List<Vulnerability> vulnerabilities = components.computeIfAbsent(auditComponent, k -> new ArrayList<>());
    if (vulnerability != null) {
      vulnerabilities.add(vulnerability);
    }
  }

  /**
   * Add vulnerabilities to the component.
   *
   * @param auditComponent  {@link AuditComponent} with vulnerability.
   * @param vulnerabilities a list of {@link Vulnerability} of the component.
   */
  public void addVulnerabilities(final AuditComponent auditComponent, final List<Vulnerability> vulnerabilities) {
    List<Vulnerability> oldVulnerabilities = components.computeIfAbsent(auditComponent, k -> new ArrayList<>());
    oldVulnerabilities.addAll(vulnerabilities);
  }

  /**
   * Add component vulnerabilities.
   *
   * @param componentsVulnerability {@link ComponentsVulnerability} which should be added.
   */
  public void addComponentsVulnerabilities(final ComponentsVulnerability componentsVulnerability) {
    components.putAll(componentsVulnerability.getAuditComponents());
  }

  @Override
  public String toString() {
    return components.toString();
  }

  @Override
  public boolean equals(final Object o) {
    if (this == o) {
      return true;
    }
    if (o == null || getClass() != o.getClass()) {
      return false;
    }
    ComponentsVulnerability that = (ComponentsVulnerability) o;
    return Objects.equals(components, that.components);
  }

  @Override
  public int hashCode() {
    return Objects.hash(components);
  }
}
